package stocc.logger;

public class Logger {

	private Logger(){}
	
	private static int maxlen = 60;
	
	private static String[] lStr = {"[CRITICAL] ",
			 						"[WARN]     ",
			 						"[INFO]     ",
            						"[DEBUG]    "};
	
	private Level lowest;
	
	static private Logger _logger;
	
	static public Logger getLogger(){
		if(_logger == null){
			_logger = new Logger();
		}
		return _logger;
	}
	
	public void log(Level level, String who, String message){
		if(level.compareTo(lowest) > 0){
			return;
		}
		
		if((who.length() + message.length()) < maxlen){
			System.out.println(lStr[level.ordinal()] + who + "::" + message);
		}else{
			String m1;
			String m2;
			int i = maxlen - who.length();
			while(true){
				char tmp = message.charAt(i);
				if(tmp == ' ' || tmp == '\\' || tmp == '/'){
					m1 = message.substring(0, i);
					m2 = message.substring(i);
					break;
				}
				i--;
			}
			System.out.println(lStr[level.ordinal()] + who + "::" + m1);
			int fillLength = lStr[1].length() + 3;
			System.out.println(fill(fillLength, ' ') + m2);
		}
		
	}
	
	public void setLevel(String level){
		lowest = Level.valueOf(level);
	}
	
	private String fill(int length, char c){
		String ret = "";
		for(int i = 0; i < length; i++){
			ret += c;
		}
		return ret;
	}

	
	/**
	 * Logging method, add this to any class that does logging
	 * @param message The message to be logged
	 * @param level The logging level of the message
	 */ /*
	private static void log(String message, Level level){
		stocc.logger.Logger.getLogger().log(level, "module_name", message);
	}	*/
}
